package com.cscj.android.utils

import android.text.format.DateUtils
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Date
import java.util.Locale

const val ONE_SECOND: Long = 1000
const val ONE_MINUTE = 60 * ONE_SECOND
const val ONE_HOUR = 60 * ONE_MINUTE
const val ONE_DAY = 24 * ONE_HOUR
const val ONE_WEEK = 7 * ONE_DAY
const val ONE_MONTH = 30 * ONE_DAY

private val notSameYearDateFormat by lazy {
    SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA)
}

private val commentDateFormat by lazy {
    SimpleDateFormat("MM/dd HH:mm", Locale.CHINA)
}

private val sameYearDateFormat by lazy {
    SimpleDateFormat("MM/dd", Locale.CHINA)
}

private val dateFormat by lazy {
    SimpleDateFormat("yyyy-MM-dd", Locale.CHINA)
}
private val dateFormat2 by lazy {
    SimpleDateFormat("yyyy/MM/dd", Locale.CHINA)
}

private val chatDateFormat by lazy {
    SimpleDateFormat("HH:mm", Locale.CHINA)
}

fun isSameDay(timestamp: Long): Boolean {
    val calendar = Calendar.getInstance()
    calendar.timeInMillis = timestamp
    val year = calendar.get(Calendar.YEAR)
    val daysOfYear = calendar.get(Calendar.DAY_OF_YEAR)

    calendar.timeInMillis = System.currentTimeMillis()
    val currentYear = calendar.get(Calendar.YEAR)
    val currentDaysOfYear = calendar.get(Calendar.DAY_OF_YEAR)

    return year == currentYear && daysOfYear == currentDaysOfYear
}

fun tomorrowTime(): Long {
    val calendar = Calendar.getInstance()
    calendar.timeInMillis = System.currentTimeMillis()
    calendar.add(Calendar.DATE, 1)
    return calendar.timeInMillis
}

fun endTimeOfDay(timestamp: Long): Long {
    val calendar = Calendar.getInstance()
    calendar.timeInMillis = timestamp
    calendar.set(Calendar.HOUR, 23)
    calendar.set(Calendar.MINUTE, 59)
    calendar.set(Calendar.SECOND, 59)
    calendar.set(Calendar.MILLISECOND, 999)
    return calendar.timeInMillis
}

private fun isYesterday(timestamp: Long): Boolean {
    val calendar = Calendar.getInstance()
    calendar.timeInMillis = timestamp
    val year = calendar.get(Calendar.YEAR)
    val daysOfYear = calendar.get(Calendar.DAY_OF_YEAR)

    calendar.timeInMillis = System.currentTimeMillis()
    val currentYear = calendar.get(Calendar.YEAR)
    val currentDaysOfYear = calendar.get(Calendar.DAY_OF_YEAR)

    return year == currentYear && currentDaysOfYear - daysOfYear == 1
}

private fun isSameWeek(timestamp: Long): Boolean {
    val calendar = Calendar.getInstance()
    calendar.timeInMillis = timestamp
    val year = calendar.get(Calendar.YEAR)
    val daysOfYear = calendar.get(Calendar.DAY_OF_YEAR)


    calendar.timeInMillis = System.currentTimeMillis()
    val currentYear = calendar.get(Calendar.YEAR)
    val currentDaysOfYear = calendar.get(Calendar.DAY_OF_YEAR)

    return currentYear == year && currentDaysOfYear - daysOfYear < 7

}

fun formatWeek(timestamp: Long): String {
    val calendar = Calendar.getInstance()
    calendar.timeInMillis = timestamp
    return when (calendar.get(Calendar.DAY_OF_WEEK)) {
        Calendar.SUNDAY -> "星期日"
        Calendar.MONDAY -> "星期一"
        Calendar.TUESDAY -> "星期二"
        Calendar.WEDNESDAY -> "星期三"
        Calendar.THURSDAY -> "星期四"
        Calendar.FRIDAY -> "星期五"
        else -> "星期六"
    }
}

fun formatDateText(date: Date?): CharSequence? {
    if (date == null) return null
    val cal = Calendar.getInstance()
    cal.time = date
    val month = cal.get(Calendar.MONTH)
    val dayOfMonth = cal.get(Calendar.DAY_OF_MONTH)
    val weekText = when (cal.get(Calendar.DAY_OF_WEEK)) {
        Calendar.SUNDAY -> "周日"
        Calendar.MONDAY -> "周一"
        Calendar.TUESDAY -> "周二"
        Calendar.WEDNESDAY -> "周三"
        Calendar.THURSDAY -> "周四"
        Calendar.FRIDAY -> "周五"
        else -> "周六"
    }

    return "${month + 1}月${dayOfMonth}日 $weekText"
}

fun formatHumanReadableMinutes(timeInMills: Long): String {
    if (timeInMills <= 0) return ""
    val diff = System.currentTimeMillis() - timeInMills
    return when {
        diff < ONE_MINUTE -> "刚刚"
        diff < ONE_HOUR -> "${diff / ONE_MINUTE}分钟前"
        diff < ONE_DAY -> "${diff / ONE_HOUR}小时前"
        diff < ONE_WEEK -> "${formatDays(diff)}天前"
        else -> {
            dateFormat2.format(Date(timeInMills))
        }
    }
}

fun formatHumanReadableDay(timeInMills: Long): String {
    if (timeInMills == 0L) return ""
    val diffMills = System.currentTimeMillis() - timeInMills
    return when {
        isSameDay(timeInMills) && diffMills < ONE_DAY -> "今天"
        isYesterday(timeInMills) -> "昨天"
        isSameWeek(timeInMills) -> {
            "${formatDays(diffMills)}天前"
        }

        isSameYear(timeInMills) -> {
            sameYearDateFormat.format(Date(timeInMills))
        }

        else -> {
            notSameYearDateFormat.format(Date(timeInMills))
        }
    }
}

fun formatConversationTime(timeInMills: Long): String {
    if (timeInMills == 0L) return ""
    val diffMills = System.currentTimeMillis() - timeInMills
    return when {
        diffMills < 5 * ONE_MINUTE -> {
            "刚刚"
        }

        diffMills < ONE_HOUR -> {
            "${formatMinutes(diffMills)}分钟前"
        }

        isSameDay(timeInMills) && diffMills < ONE_DAY -> {
            "${formatHours(diffMills)}小时前"
        }

        isYesterday(timeInMills) -> {
            "昨天${chatDateFormat.format(Date(timeInMills))}"
        }

        isSameWeek(timeInMills) -> {
            "${formatDays(diffMills)}天前"
        }

        isSameYear(timeInMills) -> {
            sameYearDateFormat.format(Date(timeInMills))
        }

        else -> {
            notSameYearDateFormat.format(Date(timeInMills))
        }

    }
}

fun formatCommentDate(date: Date): String {
    return commentDateFormat.format(date)
}

fun formatDateTime(date: Date): String {
    return notSameYearDateFormat.format(date)
}

fun caculateAge(birthday: String): Int {
    val mills = parseDate(birthday)
    if (mills == 0L) return -1
    val cal = Calendar.getInstance()
    cal.timeInMillis = System.currentTimeMillis()
    val currentYear = cal.get(Calendar.YEAR)

    cal.timeInMillis = mills
    val birthYear = cal.get(Calendar.YEAR)

    return currentYear - birthYear
}

fun formatDuration(duration: Long): String {
    val durationInSeconds = duration / ONE_SECOND
    return DateUtils.formatElapsedTime(durationInSeconds)
}


fun formatDate(birthday: Long): String {
    return dateFormat.format(Date(birthday))
}

fun parseDate(dateStr: String): Long {
    return try {
        dateFormat.parse(dateStr)?.time ?: 0
    } catch (e: Exception) {
        0L
    }
}

private fun formatSeconds(mills: Long): Long {
    return mills / ONE_SECOND
}

private fun formatMinutes(mills: Long): Long {
    return mills / ONE_MINUTE
}

private fun formatHours(mills: Long): Long {
    return mills / ONE_HOUR
}

private fun formatDays(mills: Long): Long {
    return mills / ONE_DAY
}

private fun isSameYear(timestamp: Long): Boolean {
    val calendar = Calendar.getInstance()
    calendar.timeInMillis = timestamp
    val year = calendar.get(Calendar.YEAR)

    calendar.timeInMillis = System.currentTimeMillis()
    val currentYear = calendar.get(Calendar.YEAR)

    return year == currentYear
}

fun getDayOfWeekText(dayOfWeek: Int): String {
    return when (dayOfWeek) {
        Calendar.SUNDAY -> "周日"
        Calendar.MONTH -> "周一"
        Calendar.TUESDAY -> "周二"
        Calendar.WEDNESDAY -> "周三"
        Calendar.THURSDAY -> "周四"
        Calendar.FRIDAY -> "周五"
        else -> "周六"
    }
}